package com.gdupt.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdupt.entity.ContactRecord;
import com.gdupt.entity.vo.ResponseData;
import com.gdupt.service.IContactRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 交往记录 交往记录 前端控制器
 * </p>
 *
 * @author gdupt
 * @since 2018-11-21
 */
//@RestController
@Controller
@RequestMapping("/contact-record")
public class ContactRecordController {

    @Autowired
    private IContactRecordService contactRecordService;

    @RequestMapping("/list/{customerId}")
    public @ResponseBody ResponseData list(Page page, @PathVariable(value = "customerId") Long customerId){
        List<ContactRecord> list = contactRecordService.search(page,customerId);
        return ResponseData.okPage(page.getTotal(),list);
    }

    @PostMapping("/save")
    public @ResponseBody ResponseData save(ContactRecord contactRecord){
        boolean flg = contactRecordService.saveOrUpdate(contactRecord);
        if (flg){
            return ResponseData.okMsg("添加成功");
        }
        return ResponseData.failMsg("添加失败");
    }

    @PostMapping("/del/{id}")
    public @ResponseBody ResponseData del(@PathVariable("id") Integer id){
        boolean flg = contactRecordService.removeById(id);
        if (flg){
            return ResponseData.okMsg("删除成功");
        }
        return ResponseData.failMsg("删除失败");
    }

    @PostMapping("/edit")
    public ResponseData edit(ContactRecord contactRecord){
        boolean flg = contactRecordService.saveOrUpdate(contactRecord);
        if (flg){
            return ResponseData.okMsg("编辑成功");
        }
        return ResponseData.failMsg("编辑失败");
    }

    @RequestMapping("/editPage/{id}")
    public String editPage(@PathVariable("id") Integer id,Model model){
        ContactRecord record = contactRecordService.getById(id);
        model.addAttribute("record",record);
        return "contact-record/edit-rec";////获取数据并跳转jsp页面
    }

}
